//
//  AddEntryViewController.m
//  MyVirtualDiary
//
//  Created by Alexander Wallis on 12.07.13.
//  Copyright (c) 2013 Alexander Wallis. All rights reserved.
//

#import "AddEntryViewController.h"
#import "WriteEntryViewController.h"
#import "AppDelegate.h"
#import "User.h"
#import "Entry.h"


@interface AddEntryViewController ()
@end

@implementation AddEntryViewController
@synthesize titleTextField = _titleTextField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [picker setDate:[NSDate date] animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private methods 
-(AppDelegate *)appDelegate{
    return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier]isEqualToString:@"WriteEntry"]) {
        WriteEntryViewController *destinationViewController = segue.destinationViewController;
        destinationViewController.titel = _titleTextField.text;
        destinationViewController.date = [picker date];
        destinationViewController.userID = self.userID; 
    }
}

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if ([identifier isEqualToString:@"WriteEntry"]) {
        if ([_titleTextField.text length] == 0) {
            return NO;
        }
        else
            return YES;
    }
    return YES;
}

-(IBAction)getDate:(id)sender{
    if ([_titleTextField.text length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please fill in a title" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        return; 
    }
    NSManagedObjectContext *context = [[self appDelegate]managedObjectContext];
    
    User *user = (User*)[context existingObjectWithID:self.userID error:nil];
    Entry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"Entry" inManagedObjectContext:context];
    
    NSDate *date = [picker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *theDate = [dateFormatter stringFromDate:date];
    
    entry.titel = _titleTextField.text;
    entry.date = [dateFormatter dateFromString:theDate];
    
    /*
    [user addEntriesObject:entry];
    [entry setUser:user];
     */
    
    WriteEntryViewController *writeEntryViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WriteEntryViewController"];
    writeEntryViewController.userID = self.userID;
    writeEntryViewController.titel = _titleTextField.text;
    
    [self.navigationController pushViewController:writeEntryViewController animated:YES]; 
    
    NSLog(@"Date: %@ - NAme: %@", theDate, user.name);
}

-(IBAction)cancel:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)backgroungTouched:(id)sender{
    [self.titleTextField resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField == self.titleTextField) {
        [self.titleTextField resignFirstResponder];
    }
    return NO;
}
@end
